<?php
function add_ie_html5_shim() {
    global $is_IE;
    if ($is_IE) {echo '<!--[if lt IE 9]>';
    echo '<script src="//cdn.bootcss.com/html5shiv/r29/html5.min.js"></script>';
    echo '<script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>';
    echo '<![endif]-->';
}
}
add_action('wp_footer', 'add_ie_html5_shim');

function theme_styles() {
    wp_enqueue_style('bootstrap_css', '//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css');
    // import font-awesome
    wp_enqueue_style('font_awesome', '//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css');
    if (wp_style_is('bootstrap_css', 'enqueued')) {

    /*
    如果已經載入了句柄（或者id）為'bootstrap-main'的CSS文件，就載入依存於它的自定義CSS文件
    */
    wp_enqueue_style('main_css', get_template_directory_uri() . '/style.css');
    }
    wp_enqueue_style('font_awesome_css', get_template_directory_uri() . '/css/font-awesome.css');
    wp_enqueue_style('jquery_css', get_template_directory_uri() . '/css/jquery.gallery.css');
    wp_enqueue_style('main_gallery_css', get_template_directory_uri() . '/css/main.gallery.css');
    wp_enqueue_style('popup_css', get_template_directory_uri() . '/css/popup-box.css');


}
add_action('wp_enqueue_scripts', 'theme_styles');

// 註冊JS腳本
function theme_js() {
    wp_enqueue_script('bootstrap-js', '//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js', array('jquery'), true);
    wp_enqueue_script('index-js', get_template_directory_uri() . '/js/index.js', true);
    wp_enqueue_script('jquery-1.11.3-min-js', get_template_directory_uri() . '/js/jquery-1.11.3.min.js', array('jquery'), true);
    wp_enqueue_script('jquery-1.9.1-min-js', get_template_directory_uri() . '/js/jquery-1.9.1.min.js', array('jquery'), true);
    wp_enqueue_script('jquery-min-js', get_template_directory_uri() . '/js/jquery.min.js', array('jquery'), true);
    wp_enqueue_script('jquery-mixitup-js', get_template_directory_uri() . '/js/jquery.mixitup.js', array('jquery'), true);
    wp_enqueue_script('jquery-quovolver-js', get_template_directory_uri() . '/js/jquery.quovolver.js', array('jquery'), true);
    wp_enqueue_script('jquery-quovolver-min-js', get_template_directory_uri() . '/js/jquery.quovolver.min.js', array('jquery'), true);
    wp_enqueue_script('npm-js', get_template_directory_uri() . '/js/npm.js',true);
    wp_enqueue_script('wol-carousel-js', get_template_directory_uri() . '/js/wol.carousel.js',true);


}
add_action('wp_footer', 'theme_js');

// Custom script doesn't need to enqueue
add_theme_support('menus');
add_theme_support('post-thumbnails');

function register_theme_menus() {
    register_nav_menus(array(
        'header-menu' => __('Header Menu'),
        'body-menu' => __('Body Menu'),
        'job-menu' => __('Job Menu'),
        'shop-menu' => __('Shop Menu')
        ));
}
add_action('init', 'register_theme_menus');

function create_widget($name, $id, $description) {
    /**
     * Creates a sidebar
     * @param string|array  Builds Sidebar based off of 'name' and 'id' values.
     */
    $args = array('name' => __($name), 'id' => $id, 'description' => __($description), 'class' => '', 'before_widget' => '<div class="widget">', 'after_widget' => '</div>', 'before_title' => '<h3>', 'after_title' => '</h3>');
    
    register_sidebar($args);
}


add_action( 'after_setup_theme', 'wpdocs_theme_setup' );
function wpdocs_theme_setup() {
    add_image_size( 'category-thumb', 300 ); // 300 pixels wide (and unlimited height)
    add_image_size( 'homepage-thumb', 220, 180, true ); // (cropped)
}

remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );


// function create_post_type() {
//   register_post_type( 'job',
//     array(
//       'labels' => array(
//         'name' => __( 'job' ),
//         'singular_name' => __( 'job' )
//       ),
//       'public' => true,
//       'has_archive' => true,
//       'rewrite' => array('slug' => 'products'),
//     )
//   );
//   register_post_type( 'event',
//     array(
//       'labels' => array(
//         'name' => __( 'event' ),
//         'singular_name' => __( 'event' )
//       ),
//       'public' => true,
//       // 'rewrite' => array('slug' => 'products'),
//     )
//   );
// }
// add_action( 'init', 'create_post_type' );


function powerxing_custom_posts_per_page( $query ) {
    // 為特定分類指定顯示數量，鍵為分類別名（可在後台的分類目錄中查看），值為顯示數量
    $ppp = array(
        // 比如為分類 notes 和 gallery 設置不同的顯示數量
        'carousel'   => 6,
        'works' => 8,
        );

    if ( is_home() && $query->is_tag() && $query->is_main_query() ) {
        // 獲取當前分類別名
        $slug = $query->query_vars['tag_name'];

        if ( array_key_exists($slug, $ppp) ) {
            $query->set( 'posts_per_page', $ppp[$slug] );
        }
    }
}

/**
 * ******WIDGET*******
 */

// create_widget( 'Front Page one', 'front-one', 'Displays on the one of the homepage');

/**
 * Register our sidebars and widgetized areas.
 *
 */
function arphabet_widgets_init() {

    register_sidebar( array(
        'name'          => 'Home right sidebar',
        'id'            => 'home_right_1',
        'before_widget' => '<div>',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="rounded">',
        'after_title'   => '</h2>',
        ) );

}
add_action( 'widgets_init', 'arphabet_widgets_init' );



/**
 * ******SHOP*******
 */

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {
    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;
    
    if ( ! is_admin() && is_shop() ) {

        $q->set( 'tax_query', array(array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => array( 'crowdfunding' ), // Don't display products in the knives category on the shop page
            'operator' => 'NOT IN'
            )));
    }
    remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}

/**
 * Remove breadcrum nav
 */
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0);

// unhook the WooCommerce wrappers
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);

add_action('woocommerce_before_main_content', 'my_theme_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'my_theme_wrapper_end', 10);

function my_theme_wrapper_start() {
  echo '<section id="shop">';
}
function my_theme_wrapper_end() {
  echo '</section>';
}

// Declare WooCommerce support
add_action( 'after_setup_theme', 'woocommerce_support' );
function woocommerce_support() {
    add_theme_support( 'woocommerce' );
}

function echo_wc_single_product(){
    $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );

    if ( $product_cats && ! is_wp_error ( $product_cats ) ){

        $single_cat = array_shift( $product_cats ); 
        ?>
  <h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2>
  <?php }
}
add_action( 'woocommerce_single_product_summary', 'echo_wc_single_product', 2 );
remove_action('woocommerce_single_product_summary','woocommerce_template_single_meta',40);

// Title Length Limit

function echo_loop_product_title() {
        $tag = is_product_taxonomy() || is_shop() ? 'h3' : 'h3';
        echo '<' . $tag . ' class="woocommerce-loop-product__title">' . wp_trim_words( get_the_title(), 25) . '</' . $tag . '>';
    }

add_action( 'woocommerce_shop_loop_item_title', 'echo_loop_product_title', 2);
remove_action('woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title',10);


?>
